<template>
	<view>
		<view class="prebox">
			<view class="d-f">
				<view class="preboxitem1 f28 fb">
					日期
				</view>
				<view class="preboxitem2 f28 fb">
					得分
				</view>
				<view class="preboxitem2 f28 fb">
					查看详情
				</view>
			</view>
			<scroll-view
				scroll-y 
				style="height: 100%;width: 100%;"
				:refresher-enabled="true"
				:refresher-triggered="triggered" 
				refresher-background="#F3F5F9"
				@refresherrefresh="onRefresh" 
				@refresherrestore="onRestore" 
				@scrolltolower="reachBottom"
			>
				<view v-if="list.length > 0">
					<view class="gray6" v-for="(item, index) in list" :key="index">
						<view class="preboxline">
							
						</view>
						<view class="d-f">
							<view class="preboxitem1">
								{{ item.addtime }}
							</view>
							
							<view class="preboxitem2">
								{{ item.score }}
							</view>
							<view class="preboxitem2" @tap="report(item.group_id)">
								<u-icon name="calendar" color="#288AF9" size="28"></u-icon>
							</view>
						</view>
					</view>
					<u-loadmore :status="loadStatus" />
				</view>
				<u-empty v-else mode="data"></u-empty>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page: 1,
				pageSize: 10,
				lastPage: 1,
				loadStatus: 'loadmore',
				triggered: false,
				freshing: false,
			}
		},
		onLoad(option) {
			if (!this.$store.state.isLogin) {
				uni.$u.toast('登录超时请重新登录')
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login',
					});
				}, 1500)
				return false
			} else {
				this.getList(1, this.pageSize)
			}
		},
		methods: {
			getList(page, pageSize) {
				this.$u.post(this.$u.api.examRecord, {
					page: page,
					page_size: pageSize
				}).then((res) => {
					let data = res.data
					this.page = data.current_page
					this.lastPage = data.last_page
					this.pageSize = data.per_page
					if (this.page == 1) {
						this.list = []
					}
					for (let i=0;i<data.data.length;i++) {
						let temp = data.data[i]
						this.list.push(temp)
					}
					if (data.last_page <= data.current_page) {
						this.loadStatus = 'nomore'
					} else {
						this.loadStatus = 'loadmore'
					}
				}).catch((res) => {
					console.log(res)
				})
			},
			onRefresh() {
				if (this.freshing) {
					return false
				}
				this.freshing = true
				if (!this.triggered) {
					this.triggered = true
				}
				this.getList(1, this.pageSize)
				setTimeout(() => { 
					this.triggered = false //触发onRestore，并关闭刷新图标
					this.freshing = false
				}, 1500) 
			},
			onRestore() { 
				console.log("onRestore")
			}, 
			reachBottom() {
				if (this.page < this.lastPage) {
					this.loadStatus = 'loading'
					setTimeout(() => {
						this.getList(this.page + 1, this.pageSize)
					}, 1500)
				} else {
					this.loadStatus = 'nomore'
				}
			},
			report(id) {
				uni.navigateTo({
					url: '/pages/user/exam/detail?id=' + id
				})
			},
		},
	}
</script>

<style>
	.prebox{
		width: 94%;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: 2vh auto;
	}
	.preboxline{
		width: 94%;
		height: 1rpx;
		background: #EEEEEE;
		margin: 0 auto;
	}
	.preboxitem1{
		text-align: center;
		width: 33%;
		padding: 3vh 0;
	}
	.preboxitem2{
		text-align: center;
		width: 33%;
		padding: 3vh 0;
	}
</style>
